package view;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import politic.Politica;
import politic.PoliticaAzar;
import politic.PoliticaCantidadtweets;
import politic.PoliticaSeguidores;

import com.algorithms.Algorithm1;
import com.algorithms.Recomendable;

//VS4E -- DO NOT REMOVE THIS LINE!
public class PoliticSelector extends JDialog {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel0;
	private JComboBox jComboBox0;
	private JButton jButton0;
	private JTextField jTextField0;
	private JLabel jLabel1;
	private JCheckBox jCheckBox0;
	private JTextField jTextField1;
	private JLabel jLabel2;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public PoliticSelector() {
		initComponents();
	}

	public PoliticSelector(Frame parent) {
		super(parent);
		initComponents();
	}

	public PoliticSelector(Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public PoliticSelector(Frame parent, String title) {
		super(parent, title);
		initComponents();
	}

	public PoliticSelector(Frame parent, String title, boolean modal) {
		super(parent, title, modal);
		initComponents();
	}

	public PoliticSelector(Frame parent, String title, boolean modal,
			GraphicsConfiguration arg) {
		super(parent, title, modal, arg);
		initComponents();
	}

	public PoliticSelector(Dialog parent) {
		super(parent);
		initComponents();
	}

	public PoliticSelector(Dialog parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public PoliticSelector(Dialog parent, String title) {
		super(parent, title);
		initComponents();
	}

	public PoliticSelector(Dialog parent, String title, boolean modal) {
		super(parent, title, modal);
		initComponents();
	}

	public PoliticSelector(Dialog parent, String title, boolean modal,
			GraphicsConfiguration arg) {
		super(parent, title, modal, arg);
		initComponents();
	}

	public PoliticSelector(Window parent) {
		super(parent);
		initComponents();
	}

	public PoliticSelector(Window parent, ModalityType modalityType) {
		super(parent, modalityType);
		initComponents();
	}

	public PoliticSelector(Window parent, String title) {
		super(parent, title);
		initComponents();
	}

	public PoliticSelector(Window parent, String title,
			ModalityType modalityType) {
		super(parent, title, modalityType);
		initComponents();
	}

	public PoliticSelector(Window parent, String title,
			ModalityType modalityType, GraphicsConfiguration arg) {
		super(parent, title, modalityType, arg);
		initComponents();
	}

	private void initComponents() {
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);
		setLayout(new GroupLayout());
		add(getJComboBox0(), new Constraints(new Leading(314, 177, 10, 10), new Leading(18, 10, 10)));
		add(getJTextField0(), new Constraints(new Leading(312, 34, 10, 10), new Leading(91, 12, 12)));
		add(getJLabel0(), new Constraints(new Leading(21, 258, 10, 10), new Leading(21, 25, 12, 12)));
		add(getJLabel1(), new Constraints(new Leading(140, 124, 10, 10), new Leading(87, 21, 10, 10)));
		add(getJCheckBox0(), new Constraints(new Leading(14, 10, 10), new Leading(156, 8, 8)));
		add(getJLabel2(), new Constraints(new Leading(389, 86, 12, 12), new Leading(160, 12, 12)));
		add(getJButton0(), new Constraints(new Leading(217, 10, 10), new Leading(202, 10, 10)));
		add(getJTextField1(), new Constraints(new Leading(344, 30, 12, 12), new Leading(158, 12, 12)));
		setSize(529, 259);
	}

	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("seguidores");
		}
		return jLabel2;
	}

	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setVisible(true);
			jTextField1.setEnabled(false);
		}
		return jTextField1;
	}

	private JCheckBox getJCheckBox0() {
		if (jCheckBox0 == null) {
			jCheckBox0 = new JCheckBox();
			jCheckBox0.setText("Limitar cantidad de seguidores por lista a");
			jCheckBox0.addChangeListener(new ChangeListener() {
	
				public void stateChanged(ChangeEvent event) {
					jCheckBox0ChangeStateChanged(event);
				}
			});
		}
		return jCheckBox0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Ingrese valor: ");
		}
		return jLabel1;
	}

	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
		}
		return jTextField0;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("ok!");
			jButton0.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private JComboBox getJComboBox0() {
		if (jComboBox0 == null) {
			jComboBox0 = new JComboBox();
			jComboBox0.setModel(new DefaultComboBoxModel(new Object[] { "Azar",
					"Cantidad de tweets", "Cantidad de seguidores" }));
			jComboBox0.setDoubleBuffered(false);
			jComboBox0.setBorder(null);
		}
		return jComboBox0;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Seleccione una política de filtrado");
		}
		return jLabel0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				PoliticSelector dialog = new PoliticSelector();
				dialog
						.setDefaultCloseOperation(PoliticSelector.DISPOSE_ON_CLOSE);
				dialog.setTitle("Opciones");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}

	private void jButton0MouseMouseClicked(MouseEvent event) {

		// get the politic to use.
		Politica p = getPolitic();

		// get the followers limit, if any.
		int limit = getFollowersLimit();

		boolean coolLimit = false;

		if (jCheckBox0.isSelected() && limit > -1 || !jCheckBox0.isSelected())
			coolLimit = true;

		if (p != null && coolLimit) {

			Recomendable a1 = new Algorithm1(p, limit);

			RunningDialog r = new RunningDialog(((ResultsFrame) getParent()),
					a1);
			r.setLocationRelativeTo(this);
			r.setTitle("Paciencia");
			r.setVisible(true);

			this.dispose();
		}
	}

	private int getFollowersLimit() {
		int limite = -1;
		if (jCheckBox0.isSelected()) {
			try {
				limite = Integer.parseInt(jTextField1.getText());

			} catch (NumberFormatException e) {
				ErrorDialog ed = new ErrorDialog(jTextField1.getText()
						+ " no es un un limite valido");
				ed.setTitle("Error!");
				ed.setVisible(true);
				ed.setLocationRelativeTo(this);
				jTextField1.setText("");
			}
		}
		return limite;
	}

	private Politica getPolitic() {
		Politica p = null;
		String opcion = (String) jComboBox0.getSelectedItem();
		try {
			if (opcion.equals("Cantidad de tweets")) {
				Integer min = new Integer(jTextField0.getText());
				p = new PoliticaCantidadtweets(min);

			} else if (opcion.equals("Cantidad de seguidores")) {
				int min = Integer.parseInt(jTextField0.getText());
				p = new PoliticaSeguidores(min);

			} else {
				Double min = new Double(jTextField0.getText());
				p = new PoliticaAzar(min);

			}
		} catch (NumberFormatException e) {
			ErrorDialog ed = new ErrorDialog(jTextField0.getText()
					+ " no es un número");
			ed.setTitle("Error!");
			ed.setVisible(true);
			ed.setLocationRelativeTo(this);
			jTextField0.setText("");
		}
		return p;
	}

	private void jCheckBox0ChangeStateChanged(ChangeEvent event) {
		if (jCheckBox0.isSelected()) {
			jTextField1.setEnabled(true);
		} else {
			jTextField1.setEnabled(false);
			jTextField1.setText("");
		}
	}
}